package com.boss.ccs.gis;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;

import com.boss.core.Boss;
import com.boss.core.BossConstants;
import com.boss.core.BossDescriptor;
import com.boss.core.form.BossFormHelper;
import com.boss.core.manage.BossProperty;
import com.boss.core.manage.PropertySection;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import com.boss.Messages;

import hudson.Extension;
import hudson.RelativePath;
import hudson.util.ListBoxModel;

/**
 * This class supports the GIS view.
 *
 * @author Momin Noor Khan
 */

@Extension
public class BossGIS extends Boss {
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl) super.getDescriptor();
    }

    @Override
    public String getDescription() {
        return "";
    }

    public String getProjectName() {
        return BossConstants.PROJECT_GIS;
    }

    /**
     * Finds out if the logged in user is authorized to perform GIS tasks.
     */
    public static boolean isAuthorizedGISUser() {
        return Boss.isAuthorizedGISUser();
    }

    /**
     * Gets the label name to be used when a link for this form is displayed on the post submit landing page
     */
    public String getLabel() {
        return "GIS Form";
    }

    /**
     * Gets the parent tab of this form
     */
    public String getTabView() {
        return BossConstants.TAB_GIS;
    }

    /**
     * Default action performed on pressing the submit button on the form.
     */
    public void doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
        JSONObject jsonForm = req.getSubmittedForm();
        String project = req.getParameter(BossConstants.KEY_PROJECT);
        BossGISForm form = new BossGISForm(req, jsonForm, project);

        if (form.getJsonTaskSection() == null || StringUtils.isBlank(form.getTask())) {
            BossFormHelper.setFailureMsg(req, Messages.Boss_MissingTaskSection());
            req.getView(this, getPostSubmitView()).forward(req, rsp);
            return;
        }

        if (form.isValid()) {
            form.submit();
        }
        //Display the post-submit landing page
        req.getView(this, getPostSubmitView()).forward(req, rsp);
    }

    /**
     * DescriptorImpl for the GIS Support tasks form
     */
    @Extension
    public static final class DescriptorImpl extends BossDescriptor {
        private String project = BossConstants.PROJECT_GIS;

        public String getProjectName() {
            return project;
        }

        /**
         * Gets the default target location for GIS 'copy' task from the properties file
         */
        public String getTargetLocation(String server) {
            //get the profile of the of the selected gis server
            String gisProfile = BossGlobalUtil.getGisProfilesPath() + server + ".properties";
            //from the default gis profile get the value of server home used as root
            String targetLocation = BossUtil.getPropertyValue(BossConstants.GIS_DEFAULT_SERVER_HOME_KEY, gisProfile);
            return targetLocation;
        }

        public String getTargetHelpLocation(String server) {
            //get the profile of the of the selected gis server
            String gisProfile = BossGlobalUtil.getGisProfilesPath() + server + ".properties";
            //from the default gis profile get the value of server home used as root
            String targetLocation = BossUtil.getPropertyValue("server.help.location", gisProfile);
            return targetLocation;
        }

        public List<BossProperty> getGisCustomers() {
            PropertySection propertySection = BossGlobalUtil.getPropertySection(BossConstants.GIS_CUSTOMER_KEY);
            List<BossProperty> properties = propertySection.getProperties();
            return properties;
        }

        /**
         * Populates the GIS project drop down on the form from the boss config
         */
        public ListBoxModel doFillProjectItems() {
            return getListBoxModel(project, BossConstants.PROJECTS_KEY);
        }

        /**
         * Populates the GIS release drop down on the form from the boss config
         */
        public ListBoxModel doFillReleaseItems(@QueryParameter("project") String targetProject) {
            return getListBoxModel(targetProject, BossConstants.RELEASES_KEY);
        }

        /**
         * Populates the online-help language drop down from the boss config
         */
        public ListBoxModel doFillHelpLanguageItems(@QueryParameter("project") String targetProject) {
            return getListBoxModel(BossConstants.PROJECT_CPM, BossConstants.ONLINE_HELP_LANGUAGE_KEY);
        }

        /**
         * Populates the existing build number drop down on the form
         * from the staging directory on the basis of release and project name
         */
        public ListBoxModel doFillExistingBuildNumberItems(@QueryParameter("release") @RelativePath(value = "..") String release,
                                                           @QueryParameter("project") @RelativePath(value = "..") String targetProject) {
            return getDirListBoxModel(release, BossGlobalUtil.getStagingDir(targetProject));
        }
    }
}
